﻿
namespace Triad.DAL.NorthWind
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Web;

    /// <summary>
    /// Database BASE Class
    /// </summary>
    public abstract class DBBase
    {
        static bool _EnableDataContextLogging;
        static string _ConnStr;

        /// <summary>
        /// Gets the data context.
        /// </summary>
        /// <value>The data context.</value>
        protected virtual NorthWindDataContext DataContext
        {
            get
            {
                var context = new NorthWindDataContext(ConnStr);
                if (EnableDataContextLogging)
                {
                    context.Log = HttpContext.Current.Response.Output;
                }
                //context.Connection.ConnectionString = ConnStr;
                context.ObjectTrackingEnabled = false; 

                return context;
            }
        }

        /// <summary>
        /// Gets the enable data context logging value from teh web.config.
        /// </summary>
        /// <value>The enable data context logging.</value>
        protected virtual bool EnableDataContextLogging
        {
            get
            {
                if ( _EnableDataContextLogging.Equals(null))
                {
                    string val = ConfigurationManager.AppSettings["EnableDataContextLogging"];
                    _EnableDataContextLogging = bool.Parse(val);
                }
                return _EnableDataContextLogging;
            }
        }

        /// <summary>
        /// Gets the Connectino String from the Web.Config.
        /// </summary>
        /// <value>The Conncetion String.</value>
        protected virtual string ConnStr
        {
            get
            {
                if (_ConnStr == null)
                {
                    _ConnStr = ConfigurationManager.ConnectionStrings["NorthWind"].ConnectionString;
                }
                return _ConnStr;
            }
        }

        protected virtual DataTable GetReadOnlyData(string p_strStoredProcedureName, Dictionary<string,string> p_htParameterHashTable, ref string p_strdbErrMsg)
        {
            //Accepts a Stored Procedure Name and Hashtable of parameters to call the DAL to retrieve a Read Only data table. 

            TriadDataReader objDataReader;
            //DictionaryEntry deHashTableValue; 
            SqlParameter[] sqlParams = new SqlParameter[p_htParameterHashTable.Count];

            int i = 0;
            try
            {
                objDataReader = new TriadDataReader(null,"NorthWind");
                foreach (KeyValuePair<string, string> deHashTableValue in p_htParameterHashTable)
                {
                    sqlParams[i] = new SqlParameter(Convert.ToString(deHashTableValue.Key), deHashTableValue.Value);
                    i += 1;
                }

                return objDataReader.GetData(p_strStoredProcedureName, sqlParams, ref p_strdbErrMsg);
            }

            catch (Exception ex)
            {
                p_strdbErrMsg = ex.Message;
                return null;
            }
        }

    }
}
